package com.iotechn.unimall.admin.myapi.record;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.mapper.mymapper.ICourseMapper;
import com.iotechn.unimall.data.mapper.mymapper.IRecordsMapper;
import com.iotechn.unimall.data.mapper.mymapper.IScoreMapper;
import com.iotechn.unimall.data.mapper.mymapper.IScoreRecordMapper;
import com.iotechn.unimall.mydata.domain.CourseBean;
import com.iotechn.unimall.mydata.domain.RecordsBean;
import com.iotechn.unimall.mydata.domain.ScoreBean;
import com.iotechn.unimall.mydata.domain.ScoreRecordBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IRecordServiceImpl implements IRecordService {
    @Autowired
    private IRecordsMapper iRecordsMapper;

    @Autowired
    private IScoreMapper iScoreMapper;

    @Autowired
    private IScoreRecordMapper iScoreRecordMapper;

    @Autowired
    private ICourseMapper iCourseMapper;

    @Override
    public String create(Long sid, Long parentId, Long sonId) throws ServiceException {
        RecordsBean recordsBean = new RecordsBean();
        recordsBean.setUid(sid);
        recordsBean.setCourseparentid(parentId);
        recordsBean.setCoursesonid(sonId);

        iRecordsMapper.insert(recordsBean);

        //加积分
        ScoreBean scoreBean = iScoreMapper.selectOne(new QueryWrapper<ScoreBean>().eq("uid", sid));
        if(scoreBean!=null&&scoreBean.getUid()!=null){
            Integer score = scoreBean.getScore();
            score+=5;
            scoreBean.setScore(score);
            iScoreMapper.updateById(scoreBean);
        }else{
            ScoreBean score = new ScoreBean();
            score.setUid(sid);
            score.setScore(5);
            iScoreMapper.insert(score);
        }

        //记录积分来源
        ScoreRecordBean recordBean = new ScoreRecordBean();
        recordBean.setUid(sid);
        recordBean.setMethod("完成一个课程学习加5分");
        recordBean.setScore(5);
        iScoreRecordMapper.insert(recordBean);

        return null;
    }

    @Override
    public Integer recordNum() throws ServiceException {
        Integer integer = iCourseMapper.selectCount(new QueryWrapper<CourseBean>().gt("parentId",0));
        return integer;
    }

    @Override
    public Integer studentRecordNum(Long sid) throws ServiceException {
        Integer integer = iRecordsMapper.selectCount(new QueryWrapper<RecordsBean>().eq("uid", sid));
        return integer;
    }

    @Override
    public Integer courseRecordNum(Long parentId) throws ServiceException {
        Integer integer = iCourseMapper.selectCount(new QueryWrapper<CourseBean>().eq("parentId", parentId));
        return integer;
    }

    @Override
    public Integer courseStudentRecordNum(Long parentId, Long sid) throws ServiceException {
        Integer integer = iRecordsMapper.selectCount(new QueryWrapper<RecordsBean>()
                .eq("uid", sid)
                .eq("courseParentId", parentId));
        return integer;
    }

    @Override
    public List<RecordsBean> studentRecords(Long sid, Integer pageNo, Integer limit) throws ServiceException {
        Page<RecordsBean> page = new Page<>(pageNo,limit);
        iRecordsMapper.selectPage(page,new QueryWrapper<RecordsBean>().eq("uid", sid));
        return page.getRecords();
    }

    @Override
    public Integer score(Long sid) throws ServiceException {
        ScoreBean uid = iScoreMapper.selectOne(new QueryWrapper<ScoreBean>().eq("uid", sid));
        if(uid==null){
            return 0;
        }
        return uid.getScore();
    }

    @Override
    public List<ScoreRecordBean> scoreList(Long sid) throws ServiceException {
        List<ScoreRecordBean> scoreRecordBeans = iScoreRecordMapper.selectList(new QueryWrapper<ScoreRecordBean>().eq("uid", sid));
        return scoreRecordBeans;
    }
}
